<?php

namespace app\admin\controller\ykjp\output;

use app\common\controller\Backend;
use think\Db;
use app\admin\model\Admin;
use app\admin\model\ykjp\information\basisinfo\Warehouse;
use app\admin\model\ykjp\information\basisinfo\Partition;
use app\admin\model\ykjp\purchase\Order;
use app\admin\model\ykjp\purchase\Storage;
use app\admin\model\ykjp\product\Product;
use app\admin\model\ykjp\information\basisinfo\Supplier;

/**
 * 入库数据导出
 *
 * @icon fa fa-circle-o
 */
class Purchase extends Backend {

    /**
     * Purchase模型对象
     * @var \app\admin\model\ykjp\purchase\Storage
     */
    protected $model = null;
    protected $searchFields = "name";
    protected $distinguish = true;
    protected $wherename = "";

    public function _initialize() {
        parent::_initialize();
        // $this->model = new \app\admin\model\ykjp\purchase\Storage;
        $this->model = new \app\admin\model\ykjp\purchase\Stoproduct;
    }

    /**
     * 查看
     */
    public function index() {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $this->wherename = "storage.firmid";
            $this->searchFields = "storage.code";
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['storage'])
                    ->where($where)
                    ->where("storage.status", "3")
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['storage'])
                    ->where($where)
                    ->where("storage.status", "3")
                    ->order($sort, $order)
                    ->select();
            $Storage = new Storage;
            $Admin = new Admin;
            $Warehouse = new Warehouse;
            $Partition = new Partition;
            $order = new Order;
            $Supplier = new Supplier;
            $prodcut = new Product;
            foreach ($list as $key => $row) {
                $username = $Admin->where("id", $row['storage']['admin_id'])->Field("nickname")->find();
                $row['prep_name'] = isset($username['nickname']) ? $username['nickname'] : "";
                $WareName = $Warehouse->where("id", $row['storage']['warehouse_id'])->Field("name")->find();
                $row['ware_name'] = isset($WareName['name']) ? $WareName['name'] : "";
                $PartName = $Partition->where("id", $row['storage']['partition_id'])->Field("name")->find();
                $part = isset($PartName['name']) ? $PartName['name'] : "";
                $row['ware_name'] = $row['ware_name'] . "/" . $part;
                $row['rate'] = 0;
                if ($row['storage']['purchase_id'] > 0) {
                    $orderRate = $order->where("id", $row['storage']['purchase_id'])->Field("rate")->find();
                    $row['rate'] = isset($orderRate['rate']) ? $orderRate['rate'] : 0;
                }
                $Suppliername = $Supplier->where("id", $row['storage']['supplier_id'])->Field("name,code")->find();
                $row['supplier'] = isset($Suppliername['name']) ? $Suppliername['name'] : "";
                $productName = $prodcut->where("id", $row['product_id'])->Field("name")->find();
                $row['name'] = isset($productName['name']) ? $productName['name'] : "";
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

}
